package controllers

import (
	"beego1/models"
	"fmt"
	"github.com/astaxie/beego"
	"time"
)

type UserController struct {
	beego.Controller
}

func (c *UserController) Get() {
	userAll := []models.User{}
	models.DB.Find(&userAll)
	c.Data["json"] = userAll

	c.ServeJSON()
}
func (c *UserController) Add() {
	user := models.User{
		Account:  "112233",
		Nickname: "test",
		Password: "123456",
		Sex:      1,
		State:    0,
		Ctime:    int(time.Now().Unix()),
	}
	fmt.Println(user)
	record := models.DB.NewRecord(user)
	if !record {
		beego.Info("数据插入失败")
		return
	}
	models.DB.Create(&user)
	c.Ctx.WriteString("数据插入成功")
}
func (c *UserController) Update() {
	//1、查找id=5的数据
	user := models.User{Id: 6}
	models.DB.First(&user)
	//fmt.Println(models.DB.First(&user))
	//2、执行修改
	user.Nickname = "李四"
	user.Password = "000000"
	models.DB.Save(&user)

	c.Ctx.WriteString("更新数据")
}
func (c *UserController) Del() {
	user := models.User{Id: 7}
	models.DB.Delete(&user)

	c.Ctx.WriteString("删除数据成功")
}
